<?php 
class Controller_Admin_Functions extends Controller_Abstract
{
	function actionIndex()
	{
		// 分页信息
		$pagination = Q::ini('appini/pagination');
		$numPerPage = intval($this->_context->numPerPage)?intval($this->_context->numPerPage):intval($pagination['numPerPage']);
		$pageNumShown = intval($this->_context->pageNumShown)?intval($this->_context->pageNumShown):intval($pagination['pageNumShown']);
		$currentPage = intval($this->_context->pageNum)?intval($this->_context->pageNum):1;
		// 数据查询
		$namespace = trim($this->_context->namespace);
		$find_where = '';
		if ($namespace) $find_where .= "namespace = '$namespace'";
		$data = Functions::find($find_where)
					 ->limitPage($currentPage,$numPerPage)
					 ->get();
		$count = Functions::find($find_where)->getCount('*','row_count');
		// 数据加载
		$this->_view['pagination'] = array(
			'totalCount'=>$count,
			'numPerPage'=>$numPerPage,
			'pageNumShown'=>$pageNumShown,
			'currentPage'=>$currentPage
		);
		$this->_view['default_namespace'] = $namespace;
		$this->_view['namespace'] = Functions::find()->group('namespace')->getAll();
		$this->_view['data'] = array('fetch_data' => $data , 'context'=>$this->_context);
	}

	function actionUpdate()
	{
		$functions = Helper_Functions::getInterface()->get_actions();
		//foreach file
		foreach ($functions as $module => $namespace)
		{
			foreach ($namespace as $ns => $controller)
			{
				foreach ($controller as $contro => $actions)
				{
					foreach ($actions as $item)
					{
						$in = array(
							'module'=>$module,
							'namespace'=>$ns,
							'controller'=>$contro,
							'action'=>$item,
						);
						// 添加到权限表
						if (Functions::find($in)->getCount() < 1)
						{
							$f = new Functions($in);
							$f->save();
						}
						// 功能表
						if (Module::find('module = ?',$module)->getCount() < 1)
						{
							$f = new Module($in);
							$f->save();
						}
						if (Namespaces::find('module = ? and namespace = ?',$module,$ns)->getCount() < 1)
						{
							$f = new Namespaces($in);
							$f->save();
						}
						if (Controller::find('module = ? and namespace = ? and controller = ?',$module,$ns,$contro)->getCount() < 1)
						{
							$f = new Controller($in);
							$f->save();
						}
					}
				}
			}
		}
		return json_encode(array('statusCode' => Helper_Dwz::DWZ_STATUS_OK,'message'=>'程序权限数据更新成功!','navTabId'=>'权限列表'));
	}
	/**
	 * 
	 * 删除
	 */
	function actionDelete()
	{
		$delete_id = intval($this->_context->did);
		$return_array = array('statusCode'=>Helper_Dwz::DWZ_STATUS_ERROR,'message'=>'删除失败');
		if ($delete_id){
			// 可以删除
			Functions::meta()->deleteWhere('id = ?',$delete_id);
			$return_array = array('statusCode'=>Helper_Dwz::DWZ_STATUS_OK,'message'=>'删除成功!');
		}
		return json_encode($return_array);
	}
	/**
	 * 
	 * 修改单个权限
	 */
	function actionModify()
	{
		$action = trim($this->_context->act);
		switch ($action)
		{
			case 'title':
				$modify_id = intval($this->_context->mid);
				$title = trim($this->_context->title);
				if ($modify_id){
					Functions::meta()->updateWhere(array('title'=>$title),'id = ?',$modify_id);
				}
				break;
			case 'ispower':
				$modify_id = intval($this->_context->mid);
				$ispower = intval($this->_context->ispower);
				if ($modify_id){
					Functions::meta()->updateWhere(array('ispower'=>intval(!$ispower)),'id = ?',$modify_id);
				}
				break;
			default:
				break;
		}
	}
	
	/**
	 * 功能list
	 */
	function actionProlist()
	{
		$this->_view['data'] = array(
			'module'=>Module::find()->getAll(),
			'namespace'=>Namespaces::find()->getAll(),
			'controller'=>Controller::find()->getAll(),
		);
	}
	/**
	 * 修改功能名称
	 */
	function actionModifypro()
	{
		$action = trim($this->_context->act);
		$modify_id = intval($this->_context->mid);
		$title = trim($this->_context->title);
		switch ($action)
		{
			case 'namespace':
				if ($modify_id){
					Namespaces::meta()->updateWhere(array('title'=>$title),'id = ?',$modify_id);
				}
				break;
			case 'controller':
				if ($modify_id){
					Controller::meta()->updateWhere(array('title'=>$title),'id = ?',$modify_id);
				}
				break;
			case 'module':
				if ($modify_id){
					Module::meta()->updateWhere(array('title'=>$title),'id = ?',$modify_id);
				}
				break;
			default:
				break;
		}
	}
}